package com.dev;
import android.view.ViewGroup;
import android.view.View;
import android.view.MotionEvent;
public class ScrollBar extends ViewGroup {
	MainActivity ma;
	public ScrollBar(MainActivity ma){
		super(ma);
        fingerMovedLen=(int)(3*ma.dp);
	}
	int vw,vh;
	@Override
	protected void onLayout(boolean p, int p1, int p2, int p3, int p4) {
		vw=p3-p1;
		vh=p4-p2;
		int vs=getChildCount();
		int xx=vw;
		for(int index=vs-1;index>=0;index--){
			View child=getChildAt(index);
            if(child.getVisibility()==GONE)continue;
            int wspec,hspec;
            LayoutParams lp=child.getLayoutParams();
            if(lp==null){
                wspec=MeasureSpec.makeMeasureSpec(vw,MeasureSpec.UNSPECIFIED);
                hspec=MeasureSpec.makeMeasureSpec(vh,MeasureSpec.UNSPECIFIED);
            }else{
                int ww=lp.width;
                int hh=lp.height;
                if(ww==LayoutParams.WRAP_CONTENT){
                    wspec=MeasureSpec.makeMeasureSpec(vw,MeasureSpec.UNSPECIFIED);
                }else if(ww==LayoutParams.MATCH_PARENT){
                    wspec=MeasureSpec.makeMeasureSpec(vw,MeasureSpec.EXACTLY);
                }else{
                    wspec=MeasureSpec.makeMeasureSpec(ww,MeasureSpec.EXACTLY);
                }
                if(hh==LayoutParams.WRAP_CONTENT){
                    hspec=MeasureSpec.makeMeasureSpec(vh,MeasureSpec.UNSPECIFIED);
                }else if(hh==LayoutParams.MATCH_PARENT){
                    hspec=MeasureSpec.makeMeasureSpec(vh,MeasureSpec.EXACTLY);
                }else{
                    hspec=MeasureSpec.makeMeasureSpec(hh,MeasureSpec.EXACTLY);
                }
            }
            child.measure(wspec,hspec);
			int mw=child.getMeasuredWidth();
            int mh=child.getMeasuredHeight();
			child.layout(xx-mw,0,xx,mh);
            xx-=mw;
		}
        if(xx<0){
            scrollMin=-xx;
        }else{
            scrollMin=0;
        }
	}
    int fingerMoved;
    int fingerMovedLen;
    boolean scrolling;
    int touchDownX;
    int touchDownY;
    int scrollSaved;
    int scrollMin;
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
        int ex=(int)ev.getX();
        int ey=(int)ev.getY();
        switch(ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                touchDownX=ex;
                touchDownY=ey;
                scrolling=false;
                scrollSaved=getScrollX();
                fingerMoved=0;
                super.dispatchTouchEvent(ev);
                break;
            case MotionEvent.ACTION_MOVE:
                if(fingerMoved==0){
                    int dx=ex-touchDownX;
                    int dy=ey-touchDownY;
                    int dxabs=Math.abs(dx);
                    int dyabs=Math.abs(dy);
                    if(dxabs>fingerMovedLen||dyabs>fingerMovedLen){
                        ev.setAction(MotionEvent.ACTION_CANCEL);
                        super.dispatchTouchEvent(ev);
                        if(dxabs>dyabs){
                            fingerMoved=2;
                        }else{
                            fingerMoved=1;
                        }
                    }
                }else if(fingerMoved==2){
                    int dx=ex-touchDownX;
                    int scrollWill=scrollSaved-dx;
                    if(scrollWill<=0){
                        if(scrollWill>=scrollMin){
                            setScrollX(scrollWill);
                        }else{
                            setScrollX(scrollMin);
                        }
                    }else{
                        setScrollX(0);
                    }
                }else{
                    super.dispatchTouchEvent(ev);
                }
                break;
            case MotionEvent.ACTION_UP:
                if(fingerMoved==0){
                    super.dispatchTouchEvent(ev);
                }else if(fingerMoved!=2){
                    super.dispatchTouchEvent(ev);
                }
        }
		return true;
	}
}
